﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;
using System.Xml.Serialization;
using NewLife;
using NewLife.Data;
using XCode;
using XCode.Cache;
using XCode.Configuration;
using XCode.DataAccessLayer;

namespace Datav.Design.Datas.Pocos;

/// <summary>数据源</summary>
[Serializable]
[DataObject]
[Description("数据源")]
[BindTable("p_com_source", Description = "数据源", ConnName = "datavdesign", DbType = DatabaseType.None)]
public partial class DatasSource : IDatasSource, IEntity<IDatasSource>
{
    #region 属性
    private Int64 _Id;
    /// <summary>编号</summary>
    [DisplayName("编号")]
    [Description("编号")]
    [DataObjectField(true, false, false, 0)]
    [BindColumn("Id", "编号", "", DataScale = "time")]
    public Int64 Id { get => _Id; set { if (OnPropertyChanging("Id", value)) { _Id = value; OnPropertyChanged("Id"); } } }

    private Int64 _ProjectId;
    /// <summary>案例ID</summary>
    [DisplayName("案例ID")]
    [Description("案例ID")]
    [DataObjectField(false, false, false, 0)]
    [BindColumn("ProjectId", "案例ID", "")]
    public Int64 ProjectId { get => _ProjectId; set { if (OnPropertyChanging("ProjectId", value)) { _ProjectId = value; OnPropertyChanged("ProjectId"); } } }

    private String _Type;
    /// <summary>数据类型</summary>
    [DisplayName("数据类型")]
    [Description("数据类型")]
    [DataObjectField(false, false, true, -1)]
    [BindColumn("Type", "数据类型", "")]
    public String Type { get => _Type; set { if (OnPropertyChanging("Type", value)) { _Type = value; OnPropertyChanged("Type"); } } }

    private Int64 _ComId;
    /// <summary>comId</summary>
    [DisplayName("comId")]
    [Description("comId")]
    [DataObjectField(false, false, false, 0)]
    [BindColumn("ComId", "comId", "")]
    public Int64 ComId { get => _ComId; set { if (OnPropertyChanging("ComId", value)) { _ComId = value; OnPropertyChanged("ComId"); } } }

    private String _Name;
    /// <summary>类型名称</summary>
    [DisplayName("类型名称")]
    [Description("类型名称")]
    [DataObjectField(false, false, true, 50)]
    [BindColumn("Name", "类型名称", "", Master = true)]
    public String Name { get => _Name; set { if (OnPropertyChanging("Name", value)) { _Name = value; OnPropertyChanged("Name"); } } }

    private String _Config;
    /// <summary>数据JSON</summary>
    [DisplayName("数据JSON")]
    [Description("数据JSON")]
    [DataObjectField(false, false, true, -1)]
    [BindColumn("Config", "数据JSON", "")]
    public String Config { get => _Config; set { if (OnPropertyChanging("Config", value)) { _Config = value; OnPropertyChanged("Config"); } } }

    private String _PageFilters;
    /// <summary>页面过滤器</summary>
    [DisplayName("页面过滤器")]
    [Description("页面过滤器")]
    [DataObjectField(false, false, true, -1)]
    [BindColumn("PageFilters", "页面过滤器", "")]
    public String PageFilters { get => _PageFilters; set { if (OnPropertyChanging("PageFilters", value)) { _PageFilters = value; OnPropertyChanged("PageFilters"); } } }

    private Int64 _TenantId;
    /// <summary>租户id</summary>
    [DisplayName("租户id")]
    [Description("租户id")]
    [DataObjectField(false, false, false, 0)]
    [BindColumn("TenantId", "租户id", "")]
    public Int64 TenantId { get => _TenantId; set { if (OnPropertyChanging("TenantId", value)) { _TenantId = value; OnPropertyChanged("TenantId"); } } }

    private Int32 _IsDelete;
    /// <summary>是否删除</summary>
    [DisplayName("是否删除")]
    [Description("是否删除")]
    [DataObjectField(false, false, false, 0)]
    [BindColumn("IsDelete", "是否删除", "", DefaultValue = "0")]
    public Int32 IsDelete { get => _IsDelete; set { if (OnPropertyChanging("IsDelete", value)) { _IsDelete = value; OnPropertyChanged("IsDelete"); } } }

    private DateTime _CreateTime;
    /// <summary>创建时间</summary>
    [Category("扩展")]
    [DisplayName("创建时间")]
    [Description("创建时间")]
    [DataObjectField(false, false, true, 0)]
    [BindColumn("CreateTime", "创建时间", "")]
    public DateTime CreateTime { get => _CreateTime; set { if (OnPropertyChanging("CreateTime", value)) { _CreateTime = value; OnPropertyChanged("CreateTime"); } } }

    private Int64 _CreateBy;
    /// <summary>创建人</summary>
    [Category("扩展")]
    [DisplayName("创建人")]
    [Description("创建人")]
    [DataObjectField(false, false, false, 0)]
    [BindColumn("CreateBy", "创建人", "")]
    public Int64 CreateBy { get => _CreateBy; set { if (OnPropertyChanging("CreateBy", value)) { _CreateBy = value; OnPropertyChanged("CreateBy"); } } }

    private String _CreateName;
    /// <summary>创建人名称</summary>
    [Category("扩展")]
    [DisplayName("创建人名称")]
    [Description("创建人名称")]
    [DataObjectField(false, false, true, 50)]
    [BindColumn("CreateName", "创建人名称", "")]
    public String CreateName { get => _CreateName; set { if (OnPropertyChanging("CreateName", value)) { _CreateName = value; OnPropertyChanged("CreateName"); } } }

    private DateTime _UpdateTime;
    /// <summary>更新时间</summary>
    [Category("扩展")]
    [DisplayName("更新时间")]
    [Description("更新时间")]
    [DataObjectField(false, false, true, 0)]
    [BindColumn("UpdateTime", "更新时间", "")]
    public DateTime UpdateTime { get => _UpdateTime; set { if (OnPropertyChanging("UpdateTime", value)) { _UpdateTime = value; OnPropertyChanged("UpdateTime"); } } }

    private Int64 _UpdateBy;
    /// <summary>修改建人</summary>
    [DisplayName("修改建人")]
    [Description("修改建人")]
    [DataObjectField(false, false, false, 0)]
    [BindColumn("UpdateBy", "修改建人", "")]
    public Int64 UpdateBy { get => _UpdateBy; set { if (OnPropertyChanging("UpdateBy", value)) { _UpdateBy = value; OnPropertyChanged("UpdateBy"); } } }
    #endregion

    #region 拷贝
    /// <summary>拷贝模型对象</summary>
    /// <param name="model">模型</param>
    public void Copy(IDatasSource model)
    {
        Id = model.Id;
        ProjectId = model.ProjectId;
        Type = model.Type;
        ComId = model.ComId;
        Name = model.Name;
        Config = model.Config;
        PageFilters = model.PageFilters;
        TenantId = model.TenantId;
        IsDelete = model.IsDelete;
        CreateTime = model.CreateTime;
        CreateBy = model.CreateBy;
        CreateName = model.CreateName;
        UpdateTime = model.UpdateTime;
        UpdateBy = model.UpdateBy;
    }
    #endregion

    #region 获取/设置 字段值
    /// <summary>获取/设置 字段值</summary>
    /// <param name="name">字段名</param>
    /// <returns></returns>
    public override Object this[String name]
    {
        get => name switch
        {
            "Id" => _Id,
            "ProjectId" => _ProjectId,
            "Type" => _Type,
            "ComId" => _ComId,
            "Name" => _Name,
            "Config" => _Config,
            "PageFilters" => _PageFilters,
            "TenantId" => _TenantId,
            "IsDelete" => _IsDelete,
            "CreateTime" => _CreateTime,
            "CreateBy" => _CreateBy,
            "CreateName" => _CreateName,
            "UpdateTime" => _UpdateTime,
            "UpdateBy" => _UpdateBy,
            _ => base[name]
        };
        set
        {
            switch (name)
            {
                case "Id": _Id = value.ToLong(); break;
                case "ProjectId": _ProjectId = value.ToLong(); break;
                case "Type": _Type = Convert.ToString(value); break;
                case "ComId": _ComId = value.ToLong(); break;
                case "Name": _Name = Convert.ToString(value); break;
                case "Config": _Config = Convert.ToString(value); break;
                case "PageFilters": _PageFilters = Convert.ToString(value); break;
                case "TenantId": _TenantId = value.ToLong(); break;
                case "IsDelete": _IsDelete = value.ToInt(); break;
                case "CreateTime": _CreateTime = value.ToDateTime(); break;
                case "CreateBy": _CreateBy = value.ToLong(); break;
                case "CreateName": _CreateName = Convert.ToString(value); break;
                case "UpdateTime": _UpdateTime = value.ToDateTime(); break;
                case "UpdateBy": _UpdateBy = value.ToLong(); break;
                default: base[name] = value; break;
            }
        }
    }
    #endregion

    #region 关联映射
    #endregion

    #region 扩展查询
    /// <summary>根据编号查找</summary>
    /// <param name="id">编号</param>
    /// <returns>实体对象</returns>
    public static DatasSource FindById(Int64 id)
    {
        if (id < 0) return null;

        return Find(_.Id == id);
    }
    #endregion

    #region 数据清理
    /// <summary>清理指定时间段内的数据</summary>
    /// <param name="start">开始时间。未指定时清理小于指定时间的所有数据</param>
    /// <param name="end">结束时间</param>
    /// <returns>清理行数</returns>
    public static Int32 DeleteWith(DateTime start, DateTime end)
    {
        return Delete(_.Id.Between(start, end, Meta.Factory.Snow));
    }
    #endregion

    #region 字段名
    /// <summary>取得数据源字段信息的快捷方式</summary>
    public partial class _
    {
        /// <summary>编号</summary>
        public static readonly Field Id = FindByName("Id");

        /// <summary>案例ID</summary>
        public static readonly Field ProjectId = FindByName("ProjectId");

        /// <summary>数据类型</summary>
        public static readonly Field Type = FindByName("Type");

        /// <summary>comId</summary>
        public static readonly Field ComId = FindByName("ComId");

        /// <summary>类型名称</summary>
        public static readonly Field Name = FindByName("Name");

        /// <summary>数据JSON</summary>
        public static readonly Field Config = FindByName("Config");

        /// <summary>页面过滤器</summary>
        public static readonly Field PageFilters = FindByName("PageFilters");

        /// <summary>租户id</summary>
        public static readonly Field TenantId = FindByName("TenantId");

        /// <summary>是否删除</summary>
        public static readonly Field IsDelete = FindByName("IsDelete");

        /// <summary>创建时间</summary>
        public static readonly Field CreateTime = FindByName("CreateTime");

        /// <summary>创建人</summary>
        public static readonly Field CreateBy = FindByName("CreateBy");

        /// <summary>创建人名称</summary>
        public static readonly Field CreateName = FindByName("CreateName");

        /// <summary>更新时间</summary>
        public static readonly Field UpdateTime = FindByName("UpdateTime");

        /// <summary>修改建人</summary>
        public static readonly Field UpdateBy = FindByName("UpdateBy");

        static Field FindByName(String name) => Meta.Table.FindByName(name);
    }

    /// <summary>取得数据源字段名称的快捷方式</summary>
    public partial class __
    {
        /// <summary>编号</summary>
        public const String Id = "Id";

        /// <summary>案例ID</summary>
        public const String ProjectId = "ProjectId";

        /// <summary>数据类型</summary>
        public const String Type = "Type";

        /// <summary>comId</summary>
        public const String ComId = "ComId";

        /// <summary>类型名称</summary>
        public const String Name = "Name";

        /// <summary>数据JSON</summary>
        public const String Config = "Config";

        /// <summary>页面过滤器</summary>
        public const String PageFilters = "PageFilters";

        /// <summary>租户id</summary>
        public const String TenantId = "TenantId";

        /// <summary>是否删除</summary>
        public const String IsDelete = "IsDelete";

        /// <summary>创建时间</summary>
        public const String CreateTime = "CreateTime";

        /// <summary>创建人</summary>
        public const String CreateBy = "CreateBy";

        /// <summary>创建人名称</summary>
        public const String CreateName = "CreateName";

        /// <summary>更新时间</summary>
        public const String UpdateTime = "UpdateTime";

        /// <summary>修改建人</summary>
        public const String UpdateBy = "UpdateBy";
    }
    #endregion
}
